package yzy.ecps.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import yzy.ecps.model.BtcBrand;
import yzy.ecps.service.BrandService;

@Controller
@RequestMapping("/brand")
public class BrandController {
	
	@Autowired
	private BrandService brandService;
	
	/**
	 * 品牌列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/listBrand.do")
	public String listBrand(Model model){
		List<BtcBrand> bList = brandService.findAll();
		model.addAttribute("bList", bList);
		return "item/listbrand";
	}
	/**
	 * 品牌添加页面
	 * @return
	 */
	@RequestMapping("/toAddBrand.do")
	public String toAddBrand(){
		return "item/addbrand";
	}
	
	/**
	 * 修改品牌信息页面
	 * @param brandId
	 * @param model
	 * @return
	 */
	@RequestMapping("getBrand.do")
	public String getBrand(Long brandId,Model model){
		BtcBrand brand = brandService.getById(brandId);
		model.addAttribute("brand", brand);
		return "item/editbrand";
	}
	
	/**
	 * 品牌保存
	 * @param brand
	 * @return
	 */
	@RequestMapping("/addBrand.do")
	public String addBrand(BtcBrand brand){
		brandService.saveBrand(brand);
		return "redirect:listBrand.do";
	}
	
	/**
	 * 修改品牌信息
	 * @param brand
	 * @return
	 */
	@RequestMapping("/updateBrand.do")
	public String updateBrand(BtcBrand brand){
		brandService.updateBrand(brand);
		return "redirect:listBrand.do";
	}
	
	@RequestMapping("/validBrandName.do")
	public void ajax_validBrandName(String brandName,Writer out) throws IOException{
		String result = brandService.findByName(brandName).size()>0?"yes":"no";
		out.write(result);
	}
}
